package cn.topevery.garden.dal.logic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjContractDao;
import cn.topevery.garden.entity.dto.ProjContractDTO;
import cn.topevery.garden.entity.dto.ProjContractFilesDTO;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.po.ProjContract;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.query.ProjContractQuery;
import cn.topevery.garden.entity.vo.ProjContractMoneyVO;
import cn.topevery.garden.entity.vo.ProjContractVO;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 项目合同表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjContractLogic extends BaseLogic<ProjContract> {

    @Autowired
    ProjContractDao projContractDao;

    @Autowired
    ProjFilesLogic projFilesLogic;

    @Override
    public BaseEntityDao<ProjContract, Integer> getDao() {
        return projContractDao;
    }

    public void saveOrUpdate(String userId, ProjContractFilesDTO dto) {
        dto.getContractList().stream().forEach(contract -> {
            if (ObjectUtil.isEmpty(contract.getId())) {
                contract.setContractId(UUID.randomUUID().toString());
                insert(userId, contract);
            } else {
                update(userId, contract);
            }
            //  保存附件
            if (ObjectUtil.isNotEmpty(contract.getFiles())) {
                List<ProjFiles> files = contract.getFiles().stream().filter(i -> ObjectUtil.isEmpty(i.getId())).collect(Collectors.toList());
                files.stream().forEach(i -> {
                    i.setProjId(contract.getProjId());
                    i.setProjStageId(contract.getContractId());
                });
                if (CollUtil.isNotEmpty(files)) {
                    projFilesLogic.insert(userId, files);
                }
            }
        });
        if (ObjectUtil.isNotEmpty(dto.getDeleteIds())) {
            delete(userId, dto.getDeleteIds());
        }
    }

    /**
     * 新增合同
     *
     * @param userId
     * @param dto
     */
    public void save(String userId, ProjContractDTO dto) {
        dto.setContractId(UUID.randomUUID().toString());
        dto.getFiles().stream().forEach(i -> {
            i.setProjId(dto.getProjId());
            i.setProjStageId(dto.getContractId());
        });
        insert(userId, dto);
        projFilesLogic.insert(userId, dto.getFiles());
    }

    /**
     * 获取列表
     *
     * @param query
     * @return
     */
    public List<ProjContractVO> list(ProjContractQuery query) {
        List<ProjContractVO> list = projContractDao.list(query);

        list.stream().forEach(i -> {
            List<ProjFilesVO> files = projFilesLogic.listByProjIdAndStageId(i.getProjId(), i.getContractId());
            i.setFiles(files);
        });
        return list;
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    public DataPager<ProjContractVO> listPage(ProjContractQuery query) {
        return projContractDao.listPage(query);
    }

    public ProjContract getEntityByGuidId(String guidId) {
        return projContractDao.getEntityByGuidId(guidId);
    }

    public ProjContractMoneyVO getContractMoney(ProjMoneyDTO moneyDTO) {
        if (ObjectUtil.isEmpty(moneyDTO)) {
            throw new RuntimeException("传入的对象不能为空！");
        }
        return this.projContractDao.getContractMoney(moneyDTO);
    }
}
